package com.elin.xiao

import com.baomidou.mybatisplus.annotation.DbType
import com.baomidou.mybatisplus.generator.AutoGenerator
import com.baomidou.mybatisplus.generator.InjectionConfig
import com.baomidou.mybatisplus.generator.config.DataSourceConfig
import com.baomidou.mybatisplus.generator.config.GlobalConfig
import com.baomidou.mybatisplus.generator.config.PackageConfig
import com.baomidou.mybatisplus.generator.config.StrategyConfig
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy

class CodeGenerator {

    static void main(String[] args) {
        ResourceBundle rb = ResourceBundle.getBundle("mybatis-plus")
        AutoGenerator mpg = new AutoGenerator()
        GlobalConfig gc = new GlobalConfig()
        String projectPath = System.getProperty("user.dir")
        gc.setOutputDir(projectPath + rb.getString("OutputDir"))
        gc.setOpen(false)
        gc.setBaseResultMap(true)
        gc.setBaseColumnList(true)
        gc.setKotlin(false)
        gc.setFileOverride(true)
        gc.setEnableCache(false)
        gc.setSwagger2(true)
        gc.setActiveRecord(false)
        gc.setAuthor(rb.getString("author"))
        gc.setMapperName("%sMapper")
        gc.setXmlName("%sMapper")
        gc.setServiceName("%sService")
        gc.setServiceImplName("%sServiceImpl")
        gc.setControllerName("%sController")
        mpg.setGlobalConfig(gc)
        DataSourceConfig dsc = new DataSourceConfig()
        dsc.setDbType(DbType.MYSQL)
        dsc.setUrl(rb.getString("url"))
        dsc.setDriverName("com.mysql.jdbc.Driver")
        dsc.setUsername(rb.getString("userName"))
        dsc.setPassword(rb.getString("password"))
        mpg.setDataSource(dsc)
        PackageConfig pc = new PackageConfig()
        //pc.setModuleName()
        pc.setParent(rb.getString("parent"))
        pc.setController("controller" + rb.getString("className"))
        pc.setService("service" + rb.getString("className"))
        pc.setServiceImpl("service" + rb.getString("className") + ".impl")
        pc.setEntity("entity" + rb.getString("className"))
        pc.setMapper("mapper" + rb.getString("className"))
        pc.setXml("mapper")
        mpg.setPackageInfo(pc)
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            void initMap() {

            }
        }
        mpg.setCfg(cfg)
        StrategyConfig strategy = new StrategyConfig()
        // .setCapitalMode(true)// 全局大写命名
        // .setDbColumnUnderline(true)//全局下划线命名
        strategy.setNaming(NamingStrategy.underline_to_camel)
        strategy.setColumnNaming(NamingStrategy.underline_to_camel)
        strategy.setEntityLombokModel(true)
        strategy.setRestControllerStyle(true)
        // 【实体】是否为构建者模型（默认 false）
        strategy.setEntityBuilderModel(false)
        // 自定义实体父类
        // strategy.setSuperEntityClass("com.baomidou.demo.TestEntity")
        //strategy.setTablePrefix()
        // 自定义实体，公共字段
        //strategy.setSuperEntityColumns()
        //strategy.setTableFillList()
        strategy.setInclude([rb.getString("tableName")] as String[])
        mpg.setStrategy(strategy)
        mpg.execute()
    }


}
